package utils

import (
	"github.com/google/uuid"
	gonanoid "github.com/matoous/go-nanoid/v2"
	"os"
	"os/exec"
	"path/filepath"
	"reflect"
	"sync"
	"time"
)

var curDir string
var once sync.Once

func CurrentDir() string {
	once.Do(func() {
		file, _ := exec.LookPath(os.Args[0])

		path, _ := filepath.Abs(file)
		curDir = filepath.Dir(path)
	})

	return curDir
}

func GetTimeGMT() string {
	now := time.Now()

	_, offset := now.Zone()
	return time.Unix(time.Now().Unix()-int64(offset), 0).Format("Mon, 02 Jan 2006 15:04:05 GMT")
}

func IsFileExists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}

	return false
}

func UUID() string {
	return uuid.NewString()
}

func CreateStructFromInterfacePtr(a interface{}) interface{} {
	// 获取指针类型的元素类型信息
	t := reflect.TypeOf(a).Elem()

	// 创建结构体对象
	obj := reflect.New(t).Interface()
	return obj
}

func CreateStructFromInterface(a interface{}) interface{} {
	// 获取接口的类型信息
	t := reflect.TypeOf(a)

	// 创建结构体对象
	obj := reflect.New(t).Elem().Interface()
	return obj
}

func NanoId() string {
	id, _ := gonanoid.New()
	return id
}
